# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
"""
from PyQt5.QtCore import pyqtSlot, QStringListModel
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog,QMessageBox,QAbstractItemView
from PyQt5.QtGui import QIcon
import sys, os
from Ui_main import Ui_MainWindow
from OprationExcel import ReadExcel
from showData1 import showData1

class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.setWindowTitle('读取员工缴交社保数据')
        self.setWindowIcon(QIcon('excel.ico'))
        self.txtPath.setText(os.getcwd())
        self.progressBar.hide()

        #禁止编辑
        self.fileList.setEditTriggers(QAbstractItemView.NoEditTriggers)
    
    @pyqtSlot()
    def on_btnLiulan_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        self.getFilePaht()
        self.getFileList()

    @pyqtSlot()
    def on_btnRead_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        #oe=OprationExcel()

        try:
            readExcel=ReadExcel()
            path=self.txtPath.text()
            fileModel=self.fileList.model()
            # self.progressBar.setMinimum(0)
            # self.progressBar.setMaximum(len(files))
            # self.progressBar.show()
            #result=oe.readExcel(path,files)
            if fileModel:
                files = fileModel.stringList()
                if files:
                    for file in files:
                        #readExcel.read_Excel(path,file)
                        readExcel.readExcel_by_Openpyxl(path,file)
                    # if result==True:
                    QMessageBox.information(self,'提示','读取员工缴交社保数据成功！')
                else:
                    QMessageBox.information(self, '提示', '所选择的文件夹下没有符合条件的文件，请检查！')
            else:
                QMessageBox.information(self, '提示', '所选择的文件夹下没有符合条件的文件，请检查！')
        except Exception as result:
            QMessageBox.critical(self,'错误','读取错误，原因是：{}'.format(result))

    @pyqtSlot()
    def on_btnCancel_clicked(self):
        """
        关闭窗口
        """
        self.close()
        
    @pyqtSlot()
    def on_btnShowData_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        # showData=ShowSBXX()
        # showData.showdb()
        dialog=showData1()
        dialog.exec_()

    def getFileList(self):
        files=[]
        path=self.txtPath.text()
        dirs=os.listdir(path)
        for dir in dirs:
            #if dir[-4:]=='.xls':
            if dir[-5:] == '.xlsx':
                files.append(dir)
                
        slm=QStringListModel()
        slm.setStringList(files)

        self.fileList.setModel(slm)       


    def getFilePaht(self):
        open=QFileDialog()
        path=open.getExistingDirectory(self, '选取目录', self.txtPath.text())
        #path=open.getOpenFileName()
        #print(path)
        if path:
            self.txtPath.setText(path)
        else:
            self.txtPath.setText(self.txtPath.text())

if __name__ == "__main__":    
    app = QApplication(sys.argv)
    ui = MainWindow()
    ui.show()
    sys.exit(app.exec_())
    
